﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using WeiboPlus.Models;
using WeiboPlus.Service;
using System.Collections.Generic;
using System.Linq;

namespace WeiboPlus.Renren
{
    public class RenrenJTokenToStatusMapping : MappingBase<JToken, SNSStatus>
    {
        public RenrenJTokenToStatusMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSStatus Map(JToken s)
        {
            try
            {
                var status = new RenrenSNSStatus()
                {
                    Account = _factory.Account,
                    Provider = SNSProviderType.RenRen,
                    Type = StatusType.Feed
                };

                if (s.SelectToken("comment_id") != null)
                {
                    status.Type = StatusType.Comment;
                    if (s.SelectToken("text") != null)
                    {
                        status.Text = ((string)s.SelectToken("text")).Trim('"');
                    }
                    if (s.SelectToken("time") != null)
                    {
                        var timeStr = (string)s.SelectToken("time");
                        if (timeStr.Length == "yyyy-MM-dd HH:mm:ss".Length)
                        {
                            status.CreatedTime = DateTime.ParseExact((string)s.SelectToken("time"), "yyyy-MM-dd HH:mm:ss", new CultureInfo("en-us"));
                        }
                        else if (timeStr.Length == "yyyy-MM-dd HH:mm".Length)
                        {
                            status.CreatedTime = DateTime.ParseExact((string)s.SelectToken("time"), "yyyy-MM-dd HH:mm", new CultureInfo("en-us"));
                        }
                        else
                        {
                            DateTime time;
                            var success = DateTime.TryParse(timeStr, out time);
                            if (success)
                            {
                                status.CreatedTime = time;
                            }
                        }
                    }

                }
                else
                {
                    status.FeedType = 0;
                    if (s.SelectToken("update_time") != null)
                        status.CreatedTime = DateTime.ParseExact((string)s.SelectToken("update_time"), "yyyy-MM-dd HH:mm:ss", new CultureInfo("en-us"));
                    if (s.SelectToken("feed_type") != null)
                        status.FeedType = (int)s.SelectToken("feed_type");

                    if (s.SelectToken("source_id") != null)
                    {
                        status.Id = (long)s.SelectToken("source_id");
                        status.SourceId = (long)s.SelectToken("source_id");
                    }

                    if (s.SelectToken("source_name") != null)
                        status.Source = (string)s.SelectToken("source_name");

                    switch (status.FeedType)
                    {
                        case 10:
                        case 11:
                            if (s.SelectToken("title") != null)
                                status.Text = HttpUtility.HtmlDecode((string)s.SelectToken("title"));
                            if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                            {
                                var node = s.SelectToken("attachment").First;
                                if (node.SelectToken("media_type") != null)
                                {

                                    var media_type = (string)node.SelectToken("media_type");
                                    if (media_type == "status" && node.SelectToken("media_id") != null && node.SelectToken("owner_id") != null && node.SelectToken("owner_name") != null)
                                    {
                                        status.Text = status.Text.Substring(0, status.Text.IndexOf("转自"));
                                        var restatus = new RenrenSNSStatus()
                                        {
                                            Account = _factory.Account,
                                            Provider = SNSProviderType.RenRen,
                                            Type = StatusType.Feed,
                                            Id = (long)node.SelectToken("media_id"),
                                            Source = "renren",
                                            FeedType = 10
                                        };

                                        if (node.SelectToken("content") != null)
                                        {
                                            restatus.Text = (string)node.SelectToken("content");
                                        }

                                        restatus.User = new SNSUser()
                                        {
                                            Id = (long)node.SelectToken("owner_id"),
                                            Name = (string)node.SelectToken("owner_name"),
                                            AccountID = _factory.Account.AccountID
                                        };

                                        status.RetweetedStatus = restatus;
                                    }
                                }
                            }
                            break;
                        case 20:
                        case 22:
                            if (s.SelectToken("title") != null && s.SelectToken("description") != null)
                                status.Text = (string)s.SelectToken("title") + Environment.NewLine + (string)s.SelectToken("description");
                            if (s.SelectToken("prefix") != null)
                                status.Prefix = (string)s.SelectToken("prefix");
                            break;
                        case 21:
                        case 23:
                            if (s.SelectToken("message") != null)
                                status.Text = (string)s.SelectToken("message");
                            if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                            {
                                if (s.SelectToken("prefix") != null)
                                    status.Prefix = (string)s.SelectToken("prefix");
                                var node = s.SelectToken("attachment").First;
                                if (node.SelectToken("media_type") != null)
                                {
                                    var media_type = (string)node.SelectToken("media_type");
                                    if (media_type == "blog")
                                    {
                                        var restatus = new RenrenSNSStatus()
                                        {
                                            Account = _factory.Account,
                                            Provider = SNSProviderType.RenRen,
                                            Type = StatusType.Feed,
                                            Id = (long)node.SelectToken("media_id"),
                                            Source = "renren",
                                            FeedType = 30
                                        };

                                        if (node.SelectToken("owner_id") != null && node.SelectToken("owner_name") != null)
                                            restatus.User = new SNSUser()
                                            {
                                                Id = (long)node.SelectToken("owner_id"),
                                                Name = (string)node.SelectToken("owner_name"),
                                                AccountID = _factory.Account.AccountID
                                            };
                                        else
                                        {
                                            restatus.User = new SNSUser();
                                        }

                                        if (s.SelectToken("title") != null && s.SelectToken("description") != null)
                                            restatus.Text = (string)s.SelectToken("title") + Environment.NewLine + (string)s.SelectToken("description");
                                        status.RetweetedStatus = restatus;
                                    }
                                }
                            }
                            break;
                        case 30:
                        case 31:
                            if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                            {
                                var node = s.SelectToken("attachment").First;
                                if (node.SelectToken("media_type") != null)
                                {
                                    var media_type = (string)node.SelectToken("media_type");
                                    if (media_type == "photo" && node.SelectToken("src") != null)
                                    {
                                        status.ThumbnailPic = (string)node.SelectToken("src");
                                        if (node.SelectToken("raw_src") != null)
                                            status.BmiddlePic = (string)node.SelectToken("raw_src");
                                        else
                                            status.BmiddlePic = (string)node.SelectToken("src");

                                        if (node.SelectToken("content") != null)
                                            status.Text = HttpUtility.HtmlDecode((string)node.SelectToken("content"));
                                    }
                                }
                                if (node.SelectToken("media_id") != null)
                                {
                                    status.Id = (long)node.SelectToken("media_id");
                                }
                            }
                            if (s.SelectToken("prefix") != null)
                                status.Prefix = (string)s.SelectToken("prefix");
                            break;
                        case 32:
                        case 33:
                            if (s.SelectToken("message") != null)
                                status.Text = (string)s.SelectToken("message");
                            if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                            {
                                var node = s.SelectToken("attachment").First;
                                if (node.SelectToken("media_type") != null)
                                {
                                    var media_type = (string)node.SelectToken("media_type");

                                    if (media_type == "photo" && node.SelectToken("src") != null && node.SelectToken("media_id") != null)
                                    {
                                        var restatus = new RenrenSNSStatus()
                                        {
                                            Account = _factory.Account,
                                            Provider = SNSProviderType.RenRen,
                                            Type = StatusType.Feed,
                                            Id = (long)node.SelectToken("media_id"),
                                            Source = "renren",
                                            FeedType = 30
                                        };

                                        restatus.ThumbnailPic = (string)node.SelectToken("src");
                                        if (node.SelectToken("raw_src") != null)
                                            restatus.BmiddlePic = (string)node.SelectToken("raw_src");
                                        else
                                            restatus.BmiddlePic = (string)node.SelectToken("src");

                                        if (s.SelectToken("description") != null)
                                        {
                                            restatus.Text = (string)s.SelectToken("description");
                                        }

                                        if (node.SelectToken("owner_id") != null && node.SelectToken("owner_name") != null)
                                            restatus.User = new SNSUser()
                                            {
                                                Id = (long)node.SelectToken("owner_id"),
                                                Name = (string)node.SelectToken("owner_name"),
                                                AccountID = _factory.Account.AccountID
                                            };
                                        else
                                        {
                                            restatus.User = new SNSUser();
                                        }

                                        status.RetweetedStatus = restatus;
                                    }
                                }
                                if (node.SelectToken("media_id") != null)
                                {
                                    status.Id = (long)node.SelectToken("media_id");
                                }
                            }
                            if (s.SelectToken("prefix") != null)
                                status.Prefix = (string)s.SelectToken("prefix");
                            break;
                        case 34:
                        case 35:
                        case 36:
                            break;
                        case 40:
                            break;
                        case 41:
                            if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                            {
                                var node = s.SelectToken("attachment").First;
                                if (node.SelectToken("media_type") != null)
                                {
                                    var media_type = (string)node.SelectToken("media_type");
                                    if (media_type == "page" && node.SelectToken("src") != null)
                                    {
                                        status.ThumbnailPic = (string)node.SelectToken("src");
                                        status.BmiddlePic = (string)node.SelectToken("src");

                                        if (s.SelectToken("title") != null)
                                            status.Prefix = string.Format(WeiboPlus.AppResx.BecomeFansText, _factory.Account.ServiceProvider.ToString(), (string)s.SelectToken("title"));
                                    }
                                }
                            }
                            break;
                        case 50:
                        case 51:
                        case 52:
                        case 53:
                        case 54:
                        case 55:
                            if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                            {
                                var node = s.SelectToken("attachment").First;
                                if (node.SelectToken("media_type") != null)
                                {
                                    var media_type = (string)node.SelectToken("media_type");
                                    if (media_type == "link" && node.SelectToken("src") != null)
                                    {
                                        status.ThumbnailPic = (string)node.SelectToken("src");
                                        status.BmiddlePic = (string)node.SelectToken("src");

                                        if (s.SelectToken("description") != null && s.SelectToken("prefix") != null)
                                            status.Text = (string)s.SelectToken("description");
                                    }
                                }
                            }
                            if (s.SelectToken("title") != null)
                                status.Text = HttpUtility.HtmlDecode((string)s.SelectToken("title"));
                            if (s.SelectToken("href") != null && !string.IsNullOrEmpty((string)s.SelectToken("href")))
                                status.Text += Environment.NewLine + HttpUtility.HtmlDecode((string)s.SelectToken("href"));
                            if (s.SelectToken("prefix") != null)
                                status.Prefix = (string)s.SelectToken("prefix");
                            break;
                    }



                    status.CommentCount = 0;
                    if (s.SelectToken("comments") != null && s.SelectToken("comments").SelectToken("count") != null)
                    {
                        status.CommentCount = (int)s.SelectToken("comments").SelectToken("count");
                        if (s.SelectToken("comments").SelectToken("comment") != null)
                        {
                            var comments = s.SelectToken("comments").SelectToken("comment").Select(
                    c => _factory.GetMapping<JToken, SNSStatus>().Map(c)
                    ).ToList();
                            status.Comments = comments;
                        }
                    }




                    //else if (s.SelectToken("comments") != null && s.SelectToken("comments").First != null && s.SelectToken("comments").First.SelectToken("comment_id") != null)
                    //{
                    //    foreach (var comment in s.SelectToken("comments").Values())
                    //    {
                    //        status.CommentCount++;
                    //    }
                    //}
                }

                if (s.SelectToken("uid") != null || s.SelectToken("actor_id") != null)
                    status.User = _factory.GetMapping<JToken, SNSUser>().Map(s);

                if (s.SelectToken("source") != null && s.SelectToken("source").SelectToken("text") != null)
                {
                    status.Source = (string)s.SelectToken("source").SelectToken("text");
                }
                else
                    status.Source = "renren";
                if (s.SelectToken("content") != null && string.IsNullOrEmpty(status.Text))
                {
                    status.Text = (string)s.SelectToken("content");
                    status.Type = StatusType.Comment;
                }
                return status;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                //return null;
                throw ex;
            }
        }
    }
}
